package com.hoshiicloud.market.controller.commission;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.market.entity.commission.CommissionRecordsType;
import com.hoshiicloud.market.param.commission.CommissionRecordsTypeAddParam;
import com.hoshiicloud.market.param.commission.CommissionRecordsTypeQueryParam;
import com.hoshiicloud.market.param.commission.CommissionRecordsTypeUpdateParam;
import com.hoshiicloud.market.service.commission.CommissionRecordsTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Author:campchen
 * @Date:Created in 上午 10:37 2019/7/29
 * @PACKAGENAME com.hoshiicloud.market.controller.commission
 * @Description
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/market/commission/commission-records-type")
@Api(value = "分佣流水类型", tags = {"分佣流水类型"})
public class CommissionRecordsTypeController {
    @Autowired
    private CommissionRecordsTypeService commissionRecordsTypeService;

    @GetMapping("/getPage")
    @ApiOperation(value = "分页查询佣金流水类型")
    public Response page(@RequestBody @Valid CommissionRecordsTypeQueryParam commissionRecordsTypeQueryParam) {
        try {
            QueryWrapper<CommissionRecordsType> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq(StringUtils.isNotBlank(commissionRecordsTypeQueryParam.getName()), CommissionRecordsType::getName, commissionRecordsTypeQueryParam.getName())
                    .eq((commissionRecordsTypeQueryParam.getDeleted() != null), CommissionRecordsType::getDeleted, commissionRecordsTypeQueryParam.getDeleted())
                    .eq((commissionRecordsTypeQueryParam.getStatus() != null), CommissionRecordsType::getStatus, commissionRecordsTypeQueryParam.getStatus());
            IPage<CommissionRecordsType> page = new Page<>(Long.parseLong(commissionRecordsTypeQueryParam.getPageNumber()), Long.parseLong(commissionRecordsTypeQueryParam.getPageSize()));
            String key = commissionRecordsTypeQueryParam.toString() + ",Page{pageNumber=" + commissionRecordsTypeQueryParam.getPageNumber() + ",pageSize=" + commissionRecordsTypeQueryParam.getPageSize() + "}";
            return Response.data(commissionRecordsTypeService.getPage(page, queryWrapper, key));
        } catch (Exception e) {
            log.error("分页查询佣金流水类型异常", e);
            return Response.failed(e.getMessage());
        }
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询佣金流水类型")
    public Response getCommissionRecordsTypeById(@PathVariable Long id) {
        return Response.data(commissionRecordsTypeService.getCommissionRecordsTypeById(id));
    }

    @PostMapping("/save")
    @ApiOperation(value = "新增佣金流水类型")
    public Response save(@RequestBody @Valid CommissionRecordsTypeAddParam commissionRecordsTypeAddParam) {
        CommissionRecordsType commissionRecordsType;
        try {
            commissionRecordsType = BeanUtils.covertType(commissionRecordsTypeAddParam, CommissionRecordsType.class);
        } catch (Exception e) {
            log.error("新增佣金流水Bean转换异常", e);
            return Response.failed("新增流水Bean转换异常");
        }
        return Response.data(commissionRecordsTypeService.save(commissionRecordsType));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改佣金流水类型")
    public Response update(@RequestBody @Valid CommissionRecordsTypeUpdateParam commissionRecordsUpdateParam) {
        CommissionRecordsType commissionRecordsType;
        try {
            commissionRecordsType = BeanUtils.covertType(commissionRecordsUpdateParam, CommissionRecordsType.class);
        } catch (Exception e) {
            log.error("修改佣金流水Bean转换异常", e);
            return Response.failed("新增佣金流水Bean转换异常");
        }
        return Response.data(commissionRecordsTypeService.updateById(commissionRecordsType));
    }
}
